﻿Imports System
Imports System.Data
Imports System.String
Imports System.Text
Imports System.Collections
''' <summary>
''' 退貨單類型 
''' </summary>
''' <remarks>songrenhua  2008-09-30</remarks>
Public Class Frm_OFSTBackType
    Implements IDServer.IDServer.IDServer

    Private MasterDS As DataSet
    Private DetailDS As DataSet
    Private WithEvents MainForm As Frm_MainForm


#Region "Property"

    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property

#End Region
#Region "接口"
    Public FindBool As Boolean = True


    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd

    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel

    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete

    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit

    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave

    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function
#End Region
#Region "user variabled define"

    ''' <summary>
    ''' 窗體是否加載
    ''' </summary>
    ''' <remarks></remarks>
    Private fLoadBool As Boolean = False

    Public Fields As String

#End Region

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub

    ''' <summary>
    ''' 填充狀態
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub OrdeStatus()
        Try
            Dim Dset As New DataSet
            Dim sql As String = ""
            sql &= "select ListA from BSC_OtherDefineB  where ItemNo = '16'"
            Dset = Exec(sql, E_ExecType.DataSet)
            Me.CBOrdeStatus.DataSource = Dset.Tables(0)
            Me.CBOrdeStatus.DisplayMember = "ListA"


        Catch ex As Exception
            MessageBox.Show("Initialize:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK)
        End Try
    End Sub
    ''' <summary>
    ''' 填充類型
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Type()
        Try
            Dim Dset As New DataSet
            Dim sql As String = ""
            sql &= "select ListB from BSC_OtherDefineC  where ItemNo = '100'"
            Dset = Exec(sql, E_ExecType.DataSet)
            Me.CBType.DataSource = Dset.Tables(0)
            Me.CBType.DisplayMember = "ListB"


        Catch ex As Exception
            MessageBox.Show("Initialize:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK)
        End Try
    End Sub


    Private Sub Frm_OFSTBackType_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.OrdeStatus()
        Me.Type()
        BindMasterGrid()
    End Sub
    Private Sub BindMasterGrid()
        Dim mTable As New DataTable()
        Try
            With mTable
                .Columns.Add("afSaleOrdeNo")
                .Columns.Add("fUpNo")
                .Columns.Add("fWriteNo")
                .Columns.Add("fOriginSaleNo")
                .Columns.Add("fType")
                .Columns.Add("Enname")
                .Columns.Add("SupperItem")
                .Columns.Add("Cnname")
                .Columns.Add("fRemark")
                .Columns.Add("fSumCount")
                .Columns.Add("FTAmount")
                .Columns.Add("fDSIA")
                .Columns.Add("fBSI")
                .Columns.Add("fGoodsType")
                .Columns.Add("fAreaId")
                .Columns.Add("fCustomerName")
                .Columns.Add("fLinkPhone")
                .Columns.Add("fDeliverAddress")
                .Columns.Add("fDeliverDate")
                .Columns.Add("ShopNo")
                .Columns.Add("fEmployeeId")
                .Columns.Add("fSaleDate")
                .Columns.Add("fRMoneyPsn")
                .Columns.Add("fOperaterMan")
                .Columns.Add("fOperateDate")
                .Columns.Add("fOrdeStatus")
                .Columns.Add("fCheckMan")
                .Columns.Add("fCheckDate")
                .Columns.Add("afRemark")
                .Columns.Add("fGoodsBarCode")
                .Columns.Add("Code")
                .Columns.Add("fPrice")
                .Columns.Add("fSumMoney")


            End With
            MasterDS = New DataSet()
            MasterDS.Tables.Add(mTable)
            Me.DataGridView1.DataSource = MasterDS.Tables(0)

            With Me.DataGridView1
                For i As Integer = 0 To .Columns.Count - 1
                    Dim mKey As String = .Columns(i).Name
                    Dim CaptionStr As String = BindHashTable(0).Item(mKey)
                    .Columns(i).HeaderText = CaptionStr
                Next
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            End With
        Catch ex As Exception
            MsgBox("BindMasterGrid", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mTable = Nothing
        End Try

    End Sub
    Private Function BindHashTable(Optional ByVal Index As Integer = 0) As Hashtable
        Dim mHash As New Hashtable
        Try
            With mHash
                Select Case Index

                    Case 0

                        .Add("afSaleOrdeNo", "退換單號")
                        .Add("fUpNo", "原單")
                        .Add("fWriteNo", "檔案編號")
                        .Add("fOriginSaleNo", "銷售單號")
                        .Add("fType", "類型")
                        .Add("Enname", "英文名稱")
                        .Add("SupperItem", "產家編碼")
                        .Add("Cnname", "貨品名稱")
                        .Add("fRemark", "備注")
                        .Add("fSumCount", "數量")
                        .Add("FTAmount", "總金額")
                        .Add("fDSIA", "訂金")
                        .Add("fBSI", "尚欠")
                        .Add("fGoodsType", "部門")
                        .Add("fAreaId", "區域")
                        .Add("fCustomerName", "客戶名稱")
                        .Add("fLinkPhone", "電話")
                        .Add("fDeliverAddress", "地址")
                        .Add("fDeliverDate", "送貨日期")
                        .Add("ShopNo", "門市")
                        .Add("fEmployeeId", "銷售員")
                        .Add("fSaleDate", "銷售日期")
                        .Add("fRMoneyPsn", "餘數收款人")
                        .Add("fOperaterMan", "操作員")
                        .Add("fOperateDate", "操作日期")
                        .Add("fOrdeStatus", "狀態")
                        .Add("fCheckMan", "審核員")
                        .Add("fCheckDate", "審核日期")
                        .Add("afRemark", "備注")
                        .Add("fGoodsBarCode", "條碼")
                        .Add("Code", "貨品編碼")
                        .Add("fPrice", "價格")
                        .Add("fSumMoney", "金額")

                End Select

            End With
            Return mHash
        Catch ex As Exception
            Return Nothing
            MsgBox("BindHashTable" & ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mHash = Nothing

        End Try
    End Function

    'Sub TypeTO(ByVal fType As Integer, ByVal CBType As String)
    '    ''Dim fType As Integer
    '    ''Dim CBType As String
    '    If CBType = "換貨" Then
    '        fType = 1
    '    ElseIf CBType = "換補件" Then
    '        fType = 2
    '    ElseIf CBType = "跟進" Then
    '        fType = 3
    '    ElseIf CBType = "補貨" Then
    '        fType = 4
    '    ElseIf CBType = "補補件" Then
    '        fType = 5
    '    ElseIf CBType = "後補" Then
    '        fType = 6
    '    ElseIf CBType = "收回" Then
    '        fType = 7
    '    ElseIf CBType = "補安裝" Then
    '        fType = 8
    '    Else
    '        fType = ""
    '    End If
    'End Sub

    Private Sub BtnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFind.Click
        Dim Sql As String = ""
        Dim WhereStr As String = ""
        Try

            WhereStr += " Where Isnull(flag,0)=1 "
            ''退換單單號
            If Me.TBSaleOrdeNo.Text.Trim <> "" Then
                WhereStr += "  and  a.fSaleOrdeNo ='" & Me.TBSaleOrdeNo.Text.Trim & "'"
            End If

            If Me.TBUpNo.Text.Trim <> "" Then
                WhereStr += "  and  fUpNo  ='" & Me.TBUpNo.Text.Trim & " '"
            End If


            ''銷售單號
            If Me.TBOriginSaleNo.Text.Trim <> "" Then
                WhereStr += "  and fOriginSaleNo  ='" & Me.TBOriginSaleNo.Text.Trim & "'"
            End If

            ''檔案編號
            If Me.TBWriteNo.Text.Trim <> "" Then
                WhereStr += "  and fWriteNo  ='" & Me.TBWriteNo.Text.Trim & "'"
            End If

            If Me.CBOrdeStatus.Text.Trim <> "" Then
                WhereStr += "  and  b.listA = '" & Me.CBOrdeStatus.Text.Trim & "'"
            End If

            If Me.CBType.Text.Trim <> "" Then
                WhereStr += "  and c.listB  = '" & Me.CBType.Text.Trim & "'"
            End If

            If Me.TBCustomerName.Text.Trim <> "" Then
                WhereStr += "  and fCustomerName  ='" & Me.TBCustomerName.Text.Trim & "'"
            End If

            If IsDate(Me.DateTimePicker1.Value) = True Then
                WhereStr += "  and fOperateDate>='" & Format(Me.DateTimePicker1.Value.ToString("yyyy-MM-dd"), "yyyy-MM-dd") & "'"
            End If

            If IsDate(Me.DateTimePicker2.Value) = True Then
                WhereStr += "  and fOperateDate<='" & Format(Me.DateTimePicker2.Value.ToString("yyyy-MM-dd"), "yyyy-MM-dd") & "'"
            End If

            Sql = "select  a.fSaleOrdeNo as afSaleOrdeNo,fUpNo ,fWriteNo,fOriginSaleNo, c.ListB as fType,e.Enname,e.SupperItem,Cnname ,a.fRemark ,d.fSumCount,FTAmount,fDSIA,fGoodsType,fBSI, "
            Sql &= "  fAreaId ,fCustomerName,fLinkPhone,fDeliverAddress,fDeliverDate, "
            Sql &= "fRMoneyPsn,fOperaterMan,fOperateDate,(select Top 1 ListA from  BSC_OtherDefineB  b "
            Sql &= " where ItemNo=(select Top 1 Id from BSC_OtherDefineA where ItemCode='BillstatusDefine') and  "
            Sql &= " ListCode=a.fOrdeStatus)  as fOrdeStatus,fCheckMan,fCheckDate, "
            Sql &= " Replace(d.fGoodsBarCode,'*','') as fGoodsBarCode, e.Code, "
            Sql &= " d.fRemark as afRemark,Case Isnull(d.fSumCount,0) when 0 then 0 else  Convert(Decimal(18,2),  "
            Sql &= "  d.fSumMoney/fSumCount) end as fPrice,d.fSumMoney "
            Sql &= " from Sal_SaleOrdeA  a left join  Sal_SaleOrdeB  d on a.fSaleOrdeNo = d.fSaleOrdeNo  "
            Sql &= " left join BSC_Goods e on (d.fGoodsBarCode= e.BarCode) left join  BSC_OtherDefineB b on a.fOrdeStatus = b.listCode and b.ItemNo = '16' " & vbCrLf
            Sql &= "  left join BSC_OtherDefineC c on d.fType = c.listCode  and c.ItemNo = '100'  "
            Sql &= WhereStr

            MasterDS = ReturnDataset(Sql)
            Me.DataGridView1.DataSource = MasterDS.Tables(0)

        Catch ex As Exception
            MessageBox.Show("ReturnWhereSql:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub DataGridView1_RowPostPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
        Try
            Dim DGView As DataGridView = DirectCast(sender, DataGridView)
            Dim SBrush As New SolidBrush(DGView.RowHeadersDefaultCellStyle.ForeColor)
            e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentCulture), e.InheritedRowStyle.Font, SBrush, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
        Catch ex As Exception
            MessageBox.Show("DataGridViewstorage_RowPostPaint:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub DataGridView1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGridView1.Paint
        FormatDgrd(DataGridView1, "fOrdeStatus")
    End Sub
End Class
